package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 胡慧
 * @title: ExceptionCatch
 * @projectName heima-leadnews
 * @description: 处理可控异常
 * @date 2021/2/515:22
 */
@ControllerAdvice //控制器增强类
@Slf4j
public class ExceptionCatch {
    //拦截指定的异常信息
    @ExceptionHandler(Exception.class)
    @ResponseBody //把方法的返回值转成json
    public ResponseResult exeception(Exception e){
        e.printStackTrace();
        //记录日志
        log.error("catch exception:{}", e.getMessage());
        //返回通用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
    @ExceptionHandler(CostomException.class)
    @ResponseBody
    public ResponseResult exception(CostomException e){
        e.printStackTrace();
        log.error("catch exception:{}",e.getMessage());
        return e.getResponseResult();
    }
}
